package com.moss.web;

import com.github.pagehelper.PageInfo;
import com.moss.common.core.BaseController;
import com.moss.common.domain.AjaxResult;
import com.moss.common.entity.SysCompanyEntity;
import com.moss.common.util.IdUtil;
import com.moss.system.service.CompanyService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("company")
public class CompanyController extends BaseController {

    @Resource
    CompanyService companyService;



    @GetMapping("/list")
    public AjaxResult<List<SysCompanyEntity>> companyList(SysCompanyEntity company){
        startPage();
        List<SysCompanyEntity> list = companyService.selectList(company);
        return AjaxResult.success(list,new PageInfo<>(list).getTotal());
    }


    @PostMapping("/saveCompany")
    public AjaxResult<Void> saveCompany(@RequestBody @Validated SysCompanyEntity company){
        company.setCreateBy(getUserId());
        company.setCompanyId(IdUtil.randomSnowId());
        companyService.saveCompany(company);
        return AjaxResult.success();
    }

    @GetMapping("/{companyId}")
    public AjaxResult<SysCompanyEntity> getCompany(@PathVariable String companyId){
        return AjaxResult.success(companyService.selectById(companyId));
    }

    @PostMapping("/updateCompany")
    public AjaxResult<Void> updateCompany(@RequestBody @Validated SysCompanyEntity company){
        company.setUpdateBy(getUserId());
        companyService.updateCompany(company);
        return AjaxResult.success();
    }


    @DeleteMapping("/{companyId}")
    public AjaxResult<Void> deleteCompany(@PathVariable String[] companyId){
        companyService.deleteCompany(companyId);
        return AjaxResult.success();
    }

    @PostMapping("/changeStatus")
    public AjaxResult<Void> changeStatus(@RequestBody SysCompanyEntity company ){
        company.setUpdateBy(getUserId());
        companyService.updateCompany(company);
        return AjaxResult.success();
    }
}
